// pages/fence/fence.js
Component({
  /**
   * 引入全局样式
   */
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  lifetimes: {
    created() {
      console.log('map loading..')
      wx.getLocation({
        type: 'wgs84',
        altitude: false,
        success: (result) => {
          console.log('经度：', result.longitude, ';纬度：', result.latitude)
          this.setData({
            longitude: result.longitude,
            latitude: result.latitude,
          })
        },
        fail: (err) => { console.log('获取位置失败：', err) },
        complete: () => { }    //接口调用结束的回调函数（调用成功、失败都会执行）
      });
    },
    ready() {
      this.mapCtx = wx.createMapContext('map', this)

    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    isShowState: false,
    longitude: 114.38273,    //地图默认经纬度
    latitude: 23.08383,
    isShwoDialog: false,
    markers: [{
      iconPath: '../../assets/people_loaction.png',
      id: 0,
      latitude: 23.08726464708115,
      longitude: 114.37487649200438,
      width: 38,
      height: 44,
    }],
    circles: [{
      latitude: 23.08726464708115,
      longitude: 114.37487649200438,
      color: '#f6dee190',
      fillColor: '#f6dee190',
      radius: 500,//定位点半径
      strokeWidth: 1
    }],
    patientInfo: {
      name: 'XXX',
      sex: 0,   //0 男    女   1
      address: '（未设置）',
      latitude: 23.08726464708115,
      longitude: 114.37487649200438,
      range: 500,
      state: '已暂停',
      HRV: 145,
      HR: 88,
      SL: '156'
    },

    topTips: false,
    hide: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    regionchange(e) {
      // 地图发生变化的时候，获取中间点，也就是用户选择的位置toFixed
      if (e.type == 'end' && (e.causedBy == 'scale' || e.causedBy == 'drag')) {
        // console.log(this.mapCtx)
        this.mapCtx.getCenterLocation({
          type: 'gcj02',
          success: function (res) {
            console.log('中心位置：', res.latitude, res.longitude)
          },
          fail: (err) => { console.log(err) },
        })
      }
    },

    clickLocation(e) {
      // wx.chooseLocation({
      //   success: (res) => {
      //     console.log(res)
      //   },
      //   fail: (err) => {console.log('err', err)},
      // });
      this.setData({
        isShwoDialog: true
      })
    },

    cancel(e) {
      // console.log(e.detail)
      this.setData({
        isShwoDialog: false
      })
      
      this.openToast()

    },

    chooseLoaction() {
      wx.chooseLocation({
        success: (res) => {
          console.log(res)
          this.setData({
            'patientInfo.address': res.name
          })
        },
        fail: (err) => { console.log('err', err) },
      });
    },


  }
})
